<?php
/*
Plugin Name: 微信机器人高级版
Plugin URI: http://blog.wpjam.com/
Description: 微信机器人的主要功能就是能够将你的公众账号和你的 WordPress 博客联系起来，搜索和用户发送信息匹配的日志，并自动回复用户，让你使用微信进行营销事半功倍。
Version: 5.0.1
Author: Denis
Author URI: http://blog.wpjam.com/
*/

define('WEIXIN_ROBOT_PLUGIN_URL', plugins_url('', __FILE__));
define('WEIXIN_ROBOT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WEIXIN_ROBOT_PLUGIN_FILE',  __FILE__);
define('WEIXIN_ROBOT_PLUGIN_TEMP_URL', WP_CONTENT_URL.'/uploads/weixin/');
define('WEIXIN_ROBOT_PLUGIN_TEMP_DIR', WP_CONTENT_DIR.'/uploads/weixin/');
define('WEIXIN_CUSTOM_SEND_LIMIT', time()-2*DAY_IN_SECONDS);

if(did_action('wpjam_loaded')){
	weixin_robot_loaded();
}else{
	add_action('wpjam_loaded', 'weixin_robot_loaded');
}

function weixin_robot_loaded(){
	include(WEIXIN_ROBOT_PLUGIN_DIR.'includes/trait-weixin.php');
	include(WPJAM_BASIC_PLUGIN_DIR.	'includes/class-weixin.php');					// 微信基本类	
	include(WEIXIN_ROBOT_PLUGIN_DIR.'includes/class-weixin-user.php');				// 微信用户类
	include(WEIXIN_ROBOT_PLUGIN_DIR.'includes/class-weixin-user-subscribe.php');	// 微信用户渠道类
	
	include(WEIXIN_ROBOT_PLUGIN_DIR.'public/utils.php');	// 基本函数
	include(WEIXIN_ROBOT_PLUGIN_DIR.'public/crons.php');	// 基本函数

	weixin_include_extends();		 // 扩展

	if(is_admin()){
		include(WEIXIN_ROBOT_PLUGIN_DIR.'admin/admin.php');		// 插件后台
	}

	// add_action('wpjam_module', function($module){
	// 	if(empty($module) && defined('DOING_WEIXIN_REPLY') && DOING_WEIXIN_REPLY) {	// 微信被动回复用户消息
	// 		// global $wp;
	// 		// wpjam_print_R($wp);
	// 		include(WEIXIN_ROBOT_PLUGIN_DIR.'includes/class-weixin-reply.php');		// 微信被动回复类库
	// 		include(WEIXIN_ROBOT_PLUGIN_DIR.'includes/class-weixin-message.php');	// 微信消息处理类库

	// 		include(WEIXIN_ROBOT_PLUGIN_DIR.'public/query.php');		// 搜索回复

	// 		global $wechatObj, $weixin_reply;
	// 		$weixin_reply	= $wechatObj = new WEIXIN_Reply(weixin_get_appid());
	// 		$weixin_reply->response_msg();
	// 		exit;	
	// 	}
	// });
}

add_action('plugins_loaded',function(){
	if(is_weixin()){

		include(WEIXIN_ROBOT_PLUGIN_DIR.'public/jssdk.php');		// 微信页面JSSDK

		if(wp_doing_ajax()) {
			return;
		}

		if(weixin_get_type() == 4){
			$weixin_setting	= weixin_get_setting(); 
			if(!empty($weixin_setting['weixin_oauth20'])){
				WEIXIN_User::oauth_request();	// 发起 OAuth 请求
			}
			WEIXIN_User::redirect();			// 微信活动跳转，用于支持第三方活动
		}else{
			$access_token	= $_GET['weixin_access_token']??'';
			WEIXIN_User::set_access_token_cookie($access_token);	// 订阅号就保存用户 access_token 到 cookie 里
		}
	}
});

